const { Sequelize, Model, INTEGER } = require('sequelize')
const { db } = require('../core/sequelize')
const { Mall } = require('./mall')
const { User } = require('./user')
class Order extends Model {
    //static关键字静态方法 不用new 可以直接调用
    // 添加一个订单
    static async addList(id, userId) {
        const mallDetails = await Mall.getList(id)
        const { url, name, integral } = mallDetails[0]
        //添加订单   // 修改积分
        const isSuc = await User.setIntegral(userId, integral, true)
        if (isSuc) {
            await Order.create({
                userId, url, name, integral
            })
            return true
        } else {
            return false
        }


    }
    static async getList(userId) {
        // where查询
        const list = await Order.findAll({
            where: {
                userId
            }
        })
        return list
    }
}
Order.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        url: Sequelize.STRING, //类型 字符串
        name: Sequelize.STRING, //类型 字符串
        integral: Sequelize.INTEGER, //类型 字符串
        userId: Sequelize.STRING, //类型 字符串
        createTime: Sequelize.STRING, //类型 字符串
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'order', timestamps: false
    }
)
module.exports = { Order }
